import {
  proxy,
  isAuthorization
} from '../../utils/util.js';
import LoginStore from '../login/store.js';

let startPoint;
let lastScrollTop = 0;
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '大赛', //导航栏 中间的标题
    },
    navBarHeight: app.globalData.height,
    showTipMask:false,
    urlStr:"",
    params:{},
    hiddenSearch:false,
    appParams:JSON.stringify({
      category:"contestList"
    }),
    curTabStr:'app'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  afterOnLoad:function(){
    let tabEl = this.selectComponent('#contestTab')
    tabEl.setTabs([{
      displayName: '社区活动',
      name: 'app'
    }, {
      displayName: '我的参与',
      name: 'myJoin'
    }], (item) => {
      this._tabclick({
        detail: {
          name: item.name
        }
      })
    })
  },
  onLoad: function (options) {
    let value = wx.getStorageSync('firstTipShow');
    if (!value){
      this.setData({
        showTipMask:true
      })
    }
    this.afterOnLoad();
      
  },
  _tabclick: function (e) {
    let updateParams = (urlStr)=>{
      this.setData({
        curTabStr: e.detail.name,
        urlStr: urlStr,
        params: {
          access_token: LoginStore.userInfo.access_token || "",
          page: 1,
          size: 10,
          type: 'json'
        }
      })
    };
    let item = e.detail;
    let urlStr = `${proxy}/contest/v4/data/list?listType=${item.name}`
    if (item.name == 'myJoin'){
      LoginStore.validateLogin(()=>{
        let tabEl = this.selectComponent('#contestTab');
        tabEl.setData({
          activeItem: {
            displayName: '我的参与',
            name: 'myJoin'
          }
        });
        updateParams(urlStr)
      })
    }else{
      updateParams(urlStr);
    } 
  },

  closeTipMask:function(){
    this.setData({
      showTipMask:false
    },()=>{
      wx.setStorage({
        key: "firstTipShow",
        data: true
      })
    })
  },

  stopHideMask:function(){
    console.log('cant not hide tip mask');
  },
  touchStart:function(e){
    startPoint = e.touches[0];
  },

  touchMove:function(e){
    let endPoint = e.touches[e.touches.length - 1];
    if ((endPoint.clientY - startPoint.clientY) < 0 ) {
      this.setData({
        hiddenSearch: true
      }) 
    }else{
      this.setData({
        hiddenSearch: false
      })
    }
  },
  onPageScroll:function(e){
    let curTop = e.scrollTop;
    if (curTop <0){
      curTop = 0;
    }
    let top = lastScrollTop;
    lastScrollTop = curTop;
    if ((curTop - top) > 0){
      this.setData({
        hiddenSearch: true
      }) 
    }else{
      this.setData({
        hiddenSearch: false
      })
    }
  },
  miniFail: function (e) {
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (lastScrollTop !=0){
      lastScrollTop = 0;
    }
    if (this.data.freshMark == undefined || !this.data.freshMark) {
      return;
    }

    this.afterOnLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      freshMark: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let params = this.data.params;
    params.page++;
    this.setData({
      params:params
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = LoginStore.userInfo.nickName+'邀您参与500px摄影大赛';
    return {
      title: title,
      imageUrl: "https://cdn.500px.me/images/WeChat/contestList2.png"
    }
  }
})